﻿
String.prototype.len = function() {
    return this.replace(/[^\x00-\xff]/g, "*").length;
}

function hideElms(elmTag) {
    for (i = 0; i < document.all.tags(elmTag).length; i++) {
        obj = document.all.tags(elmTag)[i];
        if (!obj || !obj.offsetParent) continue;
        obj.style.visibility = "hidden";
    }
}

//Set maxlength for multiline TextBox 
function setMaxLength(object, length) {
    var result = true;
    var tempString = object.value;
    var tt = tempString;
    if (object.BM != null) {
        if (tempString.len() != length) {
            MaxLenghValidatorUpdateDisplay(MessageDiv, object, '长度必须为' + length + "个字或字符");
            object.focus();
            result = false;
        }
        if (!result) {
            object.focus();
            return result;
        }
    }
    if (tempString.len() > length) {
        MaxLenghValidatorUpdateDisplay(MessageDiv, object, '长度不能大于' + length + "个字或字符"); //跳出提示
        object.focus();
        return false;
    }
    //        result = GetKG(object);
    //        if (!result) {
    //            MaxLenghValidatorUpdateDisplay(MessageDiv, object, '必填字段不能全部为空格'); //跳出提示
    //            object.focus();
    //            return false;
    //        }
    else {
        return true;
    }
    //return result;
}
//判断输入是否全为空格
function GetKG(object) {
    var c = 1;
    var intValue = object.value;
    if (object.ZWKG != null) {
        c = 0;
        for (var i = 0; i < intValue.length; i++) {
            if (intValue.charCodeAt(i) != 32 && intValue.charCodeAt(i) != 12288) {
                c = 1;
                break;
            }
        }
    }
    if (c != 0) {
        return true;
    }
    else {
        object.focus();
        return false;
    }
}


function ocusLength() {

    if (event.srcElement.type == "text" || event.srcElement.type == "textarea") {
        //    if(event.srcElement.type == "textarea")
        //    {
        if (event.srcElement.length != null) {
            if (event.srcElement.BM != null) {
                // hideElms('select');
                MaxLenghValidatorUpdateDisplay(MessageDiv, event.srcElement, '长度必须为' + event.srcElement.length + "个字或字符");

            }
            else {
                if (event.srcElement.value == '' || event.srcElement.value.len() > event.srcElement.length) {
                    MaxLenghValidatorUpdateDisplay(MessageDiv, event.srcElement, '长度不能大于' + event.srcElement.length + "个字或字符");
                    //  hideElms('select');
                }
            }
        }
    }
}

function CheckDateTime() {
    /*时间验证，按enter时验证时间格式是否正确*/
    /*begin**/
    allLength();
    var tag = true;
    $(".ipt_time").each(function() {
        var value = this.value;
        if (value != "") {
            var reg = /^([1-2]\d{3})[\/|\-](0?[1-9]|10|11|12)[\/|\-]([1-2]?[0-9]|0[1-9]|30|31)$/;
            var r = value.match(reg);
            if (r == null) {
                alert("请正确输入日期格式！");
                this.select();
                this.focus();
                tag = false;
                return;
            }
        }
    })

    
    if (tag == false) return false;
    else return true;
    /*end*/
}

function TimeControl() {
    var tag = true;
    $(".TextBoxStyle_LineTime").each(function() {
        var value = this.value;
        value = value.replace("年", "-")
        value = value.replace("月", "-")
        value = value.replace("日", "&")
        value = value.replace(" ", "&")
        if (value != "") {
            if (value.indexOf('&') >= 0) {
                value = value.substring(0, value.indexOf("&"));
                var reg = /^([1-2]\d{3})[\/|\-](0?[1-9]|10|11|12)[\/|\-]([1-2]?[0-9]|0[1-9]|30|31)$/;
                var r = value.match(reg);
                if (r == null) {
                  //  alert(value);
                    alert("请正确输入日期格式！");
                    this.select();
                    this.focus();
                    tag = false;
                    // return ;
                }
            }
            else {
                var reg = /^([1-2]\d{3})[\/|\-](0?[1-9]|10|11|12)[\/|\-]([1-2]?[0-9]|0[1-9]|30|31)$/;
                var r = value.match(reg);
                if (r == null) {
                    //alert(value);
                    alert("请正确输入日期格式！");
                    this.select();
                    this.focus();
                    tag = false;
                }
            }
        }
    })

    $(".TextBoxStyle_LineTime1").each(function() {
        var value = this.value;
        value = value.replace("年", "-")
        value = value.replace("月", "-")
        value = value.replace("日", "&")
        value = value.replace(" ", "&")
        if (value != "") {
            if (value.indexOf('&') >= 0) {
                value = value.substring(0, value.indexOf("&"));
                var reg = /^([1-2]\d{3})[\/|\-](0?[1-9]|10|11|12)[\/|\-]([1-2]?[0-9]|0[1-9]|30|31)$/;
                var r = value.match(reg);
                if (r == null) {
                  //  alert(value);
                    alert("请正确输入日期格式！");
                    this.select();
                    this.focus();
                    tag = false;
                    // return ;
                }
            }
            else {
                var reg = /^([1-2]\d{3})[\/|\-](0?[1-9]|10|11|12)[\/|\-]([1-2]?[0-9]|0[1-9]|30|31)$/;
                var r = value.match(reg);
                if (r == null) {
                   // alert(value);
                    alert("请正确输入日期格式！");
                    this.select();
                    this.focus();
                    tag = false;
                }
            }
        }
    })

    if (tag == false) return false;
    else return true;
}

//文本框里的长度有没有超出length有就返回false;ZHYWPT_yz_hidden
function allLength() {
    var arrAll = document.all;
    var result = true;
    var count = 0;
    for (i = 0; i < arrAll.length; i++) {//在页面里要有id为 ZHYWPT_yz_hidden的hidden控件
        if (arrAll[i].type == 'hidden' && arrAll[i].id == 'ZHYWPT_yz_hidden') {
            if (arrAll[i].value == '1') {
                break;
            }
        }
        if (arrAll[i].type == 'text' || arrAll[i].type == "textarea") {
            if (arrAll[i].length != null) {
                var resuls = setMaxLength(arrAll[i], arrAll[i].length);
                if (!resuls) {
                    count++;
                    break;
                }
            }
            if (arrAll[i].Search != null) {
                if (arrAll[i].Search == "1") {
                    arrAll[i].value = ChgSQLKeyWord_XHX(arrAll[i].value);
                }
            }
        }
    }
    if (count > 0) {
        result = false;
    }
    return result;
}
//转化文本框中的数据
function ChgSQLKeyWord_XHX(InputString) {

    var VerString = QZGH(InputString);
    if (VerString.length == 0) {
        return "";
    }
    var reg = new RegExp("^(.|[\n])*[\_]$"); //查看最后一位是否是“_”
    if (reg.test(InputString)) {
        VerString = InputString.substr(0, InputString.len() - 1) + "[_]";
        return VerString;
    }
    var regs = new RegExp("^(.|[\n])*[\%]$"); //查看最后一位是否是%
    if (regs.test(InputString)) {
        VerString = InputString.substr(0, InputString.len() - 1) + "[%]";
        return VerString;
    }
    return VerString;
}
//中括号处理
function QZGH(InString) {
    var VerString = InString;

    //    for (var i = 0; i < 5; i++) {
   VerString = VerString.replace('[', '[[]');

    // }
    return VerString;
}

//还原到文本框
function ChgInputShow() {
    var arrAll = document.all;
    for (i = 0; i < arrAll.length; i++) {

        if (arrAll[i].type == 'text' || arrAll[i].type == "textarea") {

            if (arrAll[i].Search == "1") {
                arrAll[i].value = ShowSQLKeyWord_XHX(arrAll[i].value);
            }
        }
    }
}
//还原算法
function ShowSQLKeyWord_XHX(InputString) {
    var VerString = InputString.replace('[[]', '['); ;
    if (VerString.length == 0) {
        return "";
    }
    //
    var reg1 = new RegExp("^(.|[\n])*[\[\[\]]$"); //还原最后一位是[
    if (reg1.test(InputString)) {
        VerString = InputString.substr(0, InputString.len() - 3) + "[";
        VerString = sw(VerString);
        return VerString;
    }
    //alert(VerString);
    var reg = new RegExp("^(.|[\n])*[\[\_\]]$"); //还原最后一位是_
    if (reg.test(InputString)) {
        VerString = InputString.substr(0, InputString.len() - 3) + "_";
        VerString = sw(VerString);
        return VerString;
    }
    var regs = new RegExp("^(.|[\n])*[\[\%\]]$"); //还原最后一位是%
    if (regs.test(InputString)) {
        VerString = InputString.substr(0, InputString.len() - 3) + "%";
        VerString = sw(VerString);
        return VerString;
    }
    VerString = sw(VerString);
    return VerString;
}
//处理''的方法
function sw(InputString) {
    var VerString = InputString;
    var c = ""; var j = 0;
    for (var i = 0; i < VerString.length; i) {
        j++;
        var l = 2;
        if (VerString.length < i + 2) {
            l = 1;
        }
        var v = i + 2;
        var str = VerString.substr(i, l);

        if (str == "''") {
            str = "'";
            i = i + 2;
        }
        else {
            i++;
        }
        c = c + str.substr(0, 1);
    }
    return c;

}



//对照说明
//function allLength() {
//    var arrAll = document.all; //得到面页所有的元素
//    var result = true; //默认全部var reg = new RegExp("^(.|[\n])*(\[\_\])$");saasd''''''''
//    var count = 0; //计数器
//    for (i = 0; i < arrAll.length; i++) {
//        if (arrAll[i].type == 'text' || arrAll[i].type == "textarea") {//如果是文本框
//            if (arrAll[i].length != null) {//是否有length的属性
//                var resuls = setMaxLength(arrAll[i], arrAll[i].length); //文本框里的长度有没有超出length有就返回false;
//                if (!resuls) {
//                    count++;
//                    break; //如果有一个文本框超出了就跳出
//                }
//            }
//        }
//    }
//    if (count > 0) {//如果计数器大于0则说明有不合本的文本输入
//        result = false;
//    }
//    return result;
//}
//document.documentElement.attachEvent('onkeyup', PressLength);
//document.documentElement.attachEvent('onpaste', LimitLength);
document.documentElement.attachEvent('onclick', ocusLength);

